<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑菜单</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/css.css"/>
</head>
<body>
<div class="adduser">
    <form class="login layui-form" lay-filter="editForm" style="margin-top: 10px">
        <input type="hidden" name="id"/>
        <div class="user-name layui-form-item">
            <i class="layui-icon layui-icon-layouts"></i>
            <input name="name" placeholder='菜单名称' maxlength="16" required lay-verify='required'
                   class="layui-input"/>
        </div>
        <div class="layui-form-item">
            <i class="layui-icon layui-icon-link"></i>
            <input name="url" placeholder='url' class="layui-input"/>
        </div>
        <div class="user-name layui-form-item">
            <i class="layui-icon layui-icon-face-surprised"></i>
            <input name="icon" placeholder='菜单图标(Layui图标类名)' class="layui-input" required lay-verify='required'/>
        </div>
        <div class="user-name layui-form-item">
            <i class="layui-icon layui-icon-list"></i>
            <input name="resOrder" placeholder='菜单顺序，数字小的排在前面' class="layui-input" required lay-verify='required|number'/>
        </div>
        <div class="user-name layui-form-item">
            <i class="layui-icon layui-icon-note"></i>
            <input name="note" placeholder='备注' class="layui-input"/>
        </div>
        <div class="editpassword layui-form-item">
            <label class="layui-form-label">资源类型</label>
            <div class="layui-input-block">
                <select name="type"  lay-verify="required">
                    <option value="menu">菜单</option>
                    <option value="button">按钮</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">父级菜单</label>
            <div class="layui-input-block">
                <select name="parentId" id="parentId" lay-verify="required">
                    <option value="0">顶级菜单</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="save">保存</button>
        </div>
    </form>
</div>
</body>
<script src="layui/layui.js"></script>
<script src="js/common.js" charset="utf-8"></script>
<script>
    function formRender(rowData) {
        layui.use(['form', 'jquery'], function () {
            var form = layui.form
              , $ = layui.jquery;

            form.val("editForm", {
                "id": rowData.id
                ,"name": rowData.name
                ,"url":rowData.url
                ,"resOrder":rowData.resOrder
                ,"icon":rowData.icon
                ,"note":rowData.note
                ,"type":rowData.type
            })

            //初始化父级菜单下拉框请求
            doAjax($, layer, 'GET', false, {}, '/menu/menuSelect', initParentSelect);

            //请求回调函数
            function initParentSelect(result) {
                var data = result.data;
                var htmlArr = new Array();
                if (result.status == "200") {
                    $.each(data, function (index, value) {
                        if (rowData.parentId==value.id){
                            htmlArr.push('<option value="' + value.id + '" selected>' + value.name + '</option>');
                        }else {
                            htmlArr.push('<option value="' + value.id + '">' + value.name + '</option>');
                        }
                    });
                    var htmlStr = htmlArr.join('');

                    $('#parentId').append(htmlStr);

                    //渲染表单
                    form.render();
                }
            }
        })
    }
    layui.use(['table', 'layer', 'jquery', 'form'], function () {
        var layer = layui.layer,
                form = layui.form,
                $ = layui.jquery;


        form.on('submit(save)', function (data) {

            var field = data.field;

            field.editUser=layui.data('userData').data.loginName;

            //发送添加请求
            doAjax($, layer, 'POST', false, field, '/menu/update', afterEdit);

            //保存成功回调函数
            function afterEdit(result) {
                if (result.status == "200") {
                    //保存成功的提示与跳转
                    layer.msg('编辑成功', {
                        icon: 1
                        , time: 1500
                    }, function () {
                        //iframe页面关闭自身
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        //刷新父级数据列表
                        window.parent.location.reload();
                    });

                } else {
                    layer.msg(result.msg);
                }

            }

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


    })
</script>
</html>
